package com.dengcj.epl.web.api.common;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.dengcj.epl.service.ecology.workflow.RequestIntervene;
import com.dengcj.epl.service.wechat.work.MessageUtil;
import com.dengcj.epl.system.LoadSetting;
import com.dengcj.epl.web.entity.ApiResult;
import com.dengcj.util.LogCj;

import javax.ws.rs.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dengchunjie
 * @Description: 接口测试类，用于检测基本功能是否正常
 * @date 2022/1/13 13:51
 */
@Path("/internal")
public class InternalApi {

    /**
     * 检查Jersey工作是否正常
     * @return
     */
    @GET
    @Path("/check")
    public String ready(){
        return "{\"message\":\"Jersey Is Ready\"}";
    }

    /**
     * 重新加载所有配置文件
     * @return
     */
    @GET
    @Path("/reloadsetting")
    public String reloadSetting(){
        ApiResult apiResult = new ApiResult<>();
        LoadSetting loadSetting = new LoadSetting();
        loadSetting.execute();
        return apiResult.toString();
    }

    /**
     * 提交ecology流程
     * @param requestStr
     * @return
     */
    @Path("/ecology/request/submit")
    @POST
    @Produces("application/json;charset=utf-8")
    @Consumes("application/json;charset=utf-8")
    public String testEcologyRequestInfo(String requestStr){
        JSONObject json = new JSONObject(requestStr);

        int requestid = json.getInt("requestid");
        int userid = json.getInt("userid");
        String remark = json.getStr("remark");

        RequestIntervene requestIntervene = new RequestIntervene();
        return JSONUtil.parseObj(requestIntervene.submit(requestid, userid, remark)).toString();
    }

    @Path("/workwx/sendtxtmsg")
    @POST
    @Produces("application/json;charset=utf-8")
    @Consumes("application/json;charset=utf-8")
    public String testSendWorkWxMessage(String requestStr){
        JSONObject json = new JSONObject(requestStr);
        ApiResult apiResult = new ApiResult<>();

        String userid = json.getStr("userid");
        String content = json.getStr("content");

        if(StrUtil.isBlank(userid)){
            return apiResult.error("userid为空");

            /**
             * 返回报文
             * {
             *     "msg": "userid为空",
             *     "data": {},
             *     "status": false
             * }
             */
        }

        if(StrUtil.isBlank(content)){
            return apiResult.error("content为空");
        }

        MessageUtil messageUtil = new MessageUtil();
        boolean sendStatus = messageUtil.sendMessage(userid, content);

        if(!sendStatus){
            return apiResult.error("发送失败");
        }

        return apiResult.success();
    }

    @Path("/logtest")
    @POST
    @Produces("application/json;charset=utf-8")
    @Consumes("application/json;charset=utf-8")
    public String logTest(String requestStr){
        JSONObject json = new JSONObject(requestStr);
        ApiResult apiResult = new ApiResult<>();

        String content = json.getStr("content");

        LogCj logCj = new LogCj();
        logCj.log(content);
        return apiResult.toString();
    }
}
